package rwmutex

import (
	"fmt"
	"sync"
	"time"
)

var x int64
var wg sync.WaitGroup
var rwlock sync.RWMutex

func Write() {
	rwlock.Lock()
	x = x + 1
	fmt.Println("write", x)
	rwlock.Unlock()
	//fmt.Println(runtime.Caller(0))
	wg.Done()
}
func Read() {
	rwlock.RLock()
	fmt.Print(x)
	rwlock.RUnlock()
	//fmt.Println(runtime.Caller(0))
	wg.Done()
}
func TestRWMutex() {
	start := time.Now()
	for i := 0; i < 10; i++ {
		wg.Add(1)
		//time.Sleep(time.Millisecond*1)
		go Write()
	}
	for i := 0; i < 1000; i++ {
		//time.Sleep(time.Millisecond*1)
		wg.Add(1)
		go Read()
	}
	wg.Wait()
	end := time.Now()
	fmt.Println()
	fmt.Println(end.Sub(start))
}
